package com.warehouse.module.business.service.three;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author liutao
 * @version 1.0
 * @description: TODO
 * @date 2024/7/25 19:53
 */
@Slf4j
public class ThreeService {

    public static String getImg() {
//        String url = "https://www.lufthansa-cargo.com/eservices/etracking/awb-details/-/awb/020/18222831?searchFilter=awb";
        String url = "https://www.airchinacargo.com/cargo_en/gzcx/hkyd/list/index_pc.html?waybillPrefix=999&waybillContent=07196534";
        String dataStr = HttpUtil.get(url);
        System.out.println(dataStr);
//        Document document = getDoc(url);
//        System.out.println(document);
//        return dataStr;
        return "";
    }
    public static Document getDoc(String url){
        Document doc;
        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            log.error("出现异常:{}", e.getMessage());
            return null;
        }
        return doc;
    }

    public static void main(String[] args) {
        String html = getImg();
//        getData020(html);
    }

    public static void getData020(String html) {
        Document document = Jsoup.parse(html);
//        Elements elements = document.select(".icp-cmn-toggle-content .marginB20").select("#_etrkawbdetails_WAR_etrkportlet_shipmentDetails");
//        for (int i = 0; i < elements.size(); i++) {
//            String name = elements.get(i).text();
//            System.out.println(StrUtil.format("name=={}=={}", i, name));
//        }
        Element loginform = document.getElementById("_etrkawbdetails_WAR_etrkportlet_awbDetailsModel");
        Elements inputElements = loginform.getElementsByTag("input");
        String awbDetails = "_etrkawbdetails_WAR_etrkportlet_shipmentDetails";
        String milestonePlan = "_etrkawbdetails_WAR_etrkportlet_milestoneDetails";
        for (Element inputElement : inputElements) {
            String name = inputElement.attr("name");
            String value = inputElement.attr("value");
            System.out.println("Param name: "+name+" \nParam value: "+value);
            if (StrUtil.equals(name, milestonePlan)) {
                JSONObject jsonObject = JSONUtil.parseObj(value);
                JSONArray jsonArray = jsonObject.getJSONArray("milestones");
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject milestone = jsonArray.getJSONObject(i);
                    System.out.println(StrUtil.format("milestone=={}-{}",i , milestone));
                }
            }
        }


    }
}
